﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using MSProject = Microsoft.Office.Interop.MSProject;
using JiraConnector;
using Microsoft.Office.Interop.MSProject;

namespace ProjectJiraAddIn
{
    public partial class RibbonProjectBridge
    {
        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
        {
            loadSettings(false);
        }

        private void btnHelp_Click(object sender, RibbonControlEventArgs e)
        {
            //open URL User Guide 
            String url = "https://code.google.com/p/project-bridge/wiki/User_Guide";
            System.Diagnostics.Process.Start(url);
        }

        private void btnUpdate_Click(object sender, RibbonControlEventArgs e)
        {
            //open URL Download
            String url = "https://code.google.com/p/project-bridge/wiki/Update";
            System.Diagnostics.Process.Start(url);
        }

        private void btnAbout_Click(object sender, RibbonControlEventArgs e)
        {
            AboutBoxProjectBridge about = new AboutBoxProjectBridge();
            about.ShowDialog();
        }

        private void btnImport_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                //Test add one issue to the current project 
                Microsoft.Office.Interop.MSProject.Application app = Globals.ThisAddIn.Application;
                //app.ActiveProject.Tasks.Add("New Task");


                JIRAIssueList list = JiraConnector.JIRAConnector.getJIRAIssues(Properties.Settings.Default.JIRA_Username,
                          Properties.Settings.Default.JIRA_Password,
                          Properties.Settings.Default.JIRA_URL);

                foreach (JIRAIssue issue in list.issues)
                {
                    app.ActiveProject.Tasks.Add(issue.key + " " + issue.fields["summary"]);
                    int count = app.ActiveProject.Tasks.Count;
                    Task t = app.ActiveProject.Tasks[count];

                    //t.Name = issue.key;
                    t.Duration = issue.aggregatetimeestimate() * 60;
                    t.ActualDuration = issue.aggregatetimespent() * 60;
                    t.Start = issue.startDate();
                    //t.Finish = issue.dueDate();
                    t.Hyperlink = issue.hyperlink();
                    t.ResourceNames = issue.assignee();
                }
            }
            catch (System.Exception ex)
            {
                System.Console.Write("Error:" + ex.Message);
            }
        }

        private void btnSettings_Click(object sender, RibbonControlEventArgs e)
        {
            loadSettings(true);
        }

        private static void loadSettings(bool showDialog)
        {
            JiraConnector.Settings s = new JiraConnector.Settings();
            if (showDialog) 
                s.ShowDialog();

            Properties.Settings.Default.JIRA_Username = s.JIRA_Username();
            Properties.Settings.Default.JIRA_Password = s.JIRA_Password();
            Properties.Settings.Default.JIRA_URL = s.JIRA_URL();
        }

        private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            JiraConnector.Main s = new JiraConnector.Main();
            s.ShowDialog();
        }
    }
}
